package ua.kharkov.knure.pereverziev.diploma.db.entity;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

@Entity(name = "tickets")
public class Ticket extends HasId {

	private static final long serialVersionUID = -1L;

	@Id
	@Column(name = "ticket_id")
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long id;

	Date purchaseDate;

	@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinTable(name = "ticket_flights")
	private List<Flight> flights = new ArrayList<Flight>(0);

	@ManyToOne
	@JoinColumn(name = "user_id")
	private User user;

	@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinTable(name = "ticket_services")
	private List<AdditionalService> services = new ArrayList<AdditionalService>(
			0);

	private Float price;

	@Override
	public String toString() {
		return "Ticket [flight=" + flights + ", user=" + user + ", services="
				+ services + "]";
	}

	// *********************************************************
	// ****************** Getters and setters ******************

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Ticket other = (Ticket) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	public Float getServicesPrice() {
		Float result = 0f;
		for (final AdditionalService service : services) {
			result += service.getPrice();
		}
		return result;
	}

	public Float getCalculatedPrice() {
		Float result = 0f;
		for (final AdditionalService service : services) {
			result += service.getPrice();
		}
		for (final Flight flight : flights) {
			result += flight.getPrice();
		}
		return result;
	}

	@Override
	public Long getId() {
		return id;
	}

	@Override
	public void setId(final Long id) {
		this.id = id;
	}

	public List<Flight> getFlights() {
		return flights;
	}

	public void setFlights(final List<Flight> flights) {
		this.flights = flights;
	}

	public User getUser() {
		return user;
	}

	public void setUser(final User user) {
		this.user = user;
	}

	public List<AdditionalService> getServices() {
		return services;
	}

	public void setServices(final List<AdditionalService> services) {
		this.services = services;
	}

	public Float getPrice() {
		return price;
	}

	public void setPrice(final Float price) {
		this.price = price;
	}

	public String getAdditioalServices() {
		final StringBuilder result = new StringBuilder();
		for (final AdditionalService service : services) {
			result.append(service);
		}
		return result.toString();
	}

	public void generatePrice() {
		Float result = getCalculatedPrice();
		result *= (100 - user.getStatus().getSaving()) / 100;
		this.price = result;
	}

	public Date getPurchaseDate() {
		return purchaseDate;
	}

	public void setPurchaseDate(Date purchaseDate) {
		this.purchaseDate = purchaseDate;
	}

}
